package
{
	import as3isolib.display.IsoSprite;
	import as3isolib.display.scene.IsoScene;
	
	import de.polygonal.math.PM_PRNG;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;

	public class SectorController
	{
		private var scene:IsoScene;
		private var m_renderedSections:Vector.<Rectangle> = new Vector.<Rectangle>;
		private var m_currentSection:Rectangle;		
		
		public function SectorController(scene:IsoScene)
		{
			this.scene = scene;
			m_currentSection = new Rectangle(0,0,Game.SECTOR_SIZE, Game.SECTOR_SIZE);
			
			render();	
			this.scene.render(true);
		}
		
		public function setPosition(pos:Point):void{
			pos = Game.clampPoint(pos, Game.SECTOR_SIZE);
			
			if(!m_currentSection.containsPoint(pos)){
				m_currentSection.x = pos.x;
				m_currentSection.y = pos.y;
				
				render();
				this.scene.render(true);
			}
		}
		
		public function render():void{
			var sectionsToBeRendered:Vector.<Rectangle> = new Vector.<Rectangle>;
			var i:int, j:int, il:int, jl:int;
			var section:Rectangle;
			for(i = -1; i < 2; i++){
				for(j = -1; j < 2; j++){
					section = new Rectangle(m_currentSection.x + i*Game.SECTOR_SIZE, m_currentSection.y + j*Game.SECTOR_SIZE, Game.SECTOR_SIZE, Game.SECTOR_SIZE);
					sectionsToBeRendered.push(section);
				}
			}
			
			for(i = 0, il = sectionsToBeRendered.length; i < il; i++){
				section = sectionsToBeRendered[i];
				if(!isAlreadyRendered(section)){
					renderSection(section);
				}
			}
			
			for(i = 0; i < m_renderedSections.length; i++){
				section = m_renderedSections[i];
				if(m_currentSection.topLeft.subtract(section.topLeft).length > Game.SECTOR_SIZE*3){
					var gameObjects:Vector.<IsoSprite> = m_gameObjectsBySection[section];
					for(j = 0; j < gameObjects.length; j++){
						this.scene.removeChild(gameObjects[j]);
					}
					delete m_gameObjectsBySection[section];		
					m_renderedSections.splice(m_renderedSections.indexOf(section),1);
				}
			}
			
			
		}
		
		private var m_gameObjectsBySection:Dictionary = new Dictionary(true);
		
		private function renderSection(section:Rectangle):void{
			var i:int, j:int;
			var tree:IsoSprite;
			var seed:String;
			var pr:PM_PRNG = new PM_PRNG;
			pr.seed = section.x * 1000 + section.y;
			
			var gameObjects:Vector.<IsoSprite> = new Vector.<IsoSprite>;
			
			for(i = section.left; i < section.right; i += 50){
				for(j = section.top; j < section.bottom; j+= 50){
					if(Math.floor(pr.nextDouble()*20) == 1){
						tree = Game.createIsoSprite('assets/redwood.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					} else if(Math.floor(pr.nextDouble()*50) == 1){
						tree = Game.createIsoSprite('assets/willow.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					} else if(Math.floor(pr.nextDouble()*100) == 1){
						tree = Game.createIsoSprite('assets/baretree.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					} else if(Math.floor(pr.nextDouble()*50) == 1){
						tree = Game.createIsoSprite('assets/blueblossomtree.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					}  else if(Math.floor(pr.nextDouble()*50) == 1){
						tree = Game.createIsoSprite('assets/maple.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					} else if(Math.floor(pr.nextDouble()*50) == 1){
						tree = Game.createIsoSprite('assets/pointseta.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					} else if(Math.floor(pr.nextDouble()*50) == 1){
						tree = Game.createIsoSprite('assets/pinata.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					} 
					else if(Math.floor(pr.nextDouble()*50) == 1){
						tree = Game.createIsoSprite('assets/rocks.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					}
					else if(Math.floor(pr.nextDouble()*50) == 1){
						tree = Game.createIsoSprite('assets/springflowers.png');
						tree.x = i;
						tree.y = j;
						this.scene.addChild(tree);
						gameObjects.push(tree);
					}
				}
			}
			
			m_gameObjectsBySection[section] = gameObjects;
			
			m_renderedSections.push(section);
			
		}
		
		private function isAlreadyRendered(section:Rectangle):Boolean{
			var result:Boolean = false;
			var i:int, il:int;
			for(i = 0, il = m_renderedSections.length; i < il; i++){
				if(m_renderedSections[i].equals(section)){
					return true;
				}
			}
			
			return false;
		}		
	}
}